//localStorage 获取数组对象的方法
import router from "@/router";
import Layout from '@/layout'

const _import = require('../router/_import_' +process.env.NODE_ENV)


export function getObjArr(name){
    return JSON.parse(window.sessionStorage.getItem(name));
}

//localStorage 存储数组对象的方法
export function saveObjArr(name, data) {
    sessionStorage.setItem(name, JSON.stringify(data))
}

export function routerGo(to, next,routers) {
    let getRouter = filterAsyncRouter(routers) //过滤路由
    router.addRoutes(getRouter) //动态添加路由
    global.antRouter = getRouter //将路由数据传递给全局变量，做侧边栏菜单渲染工作
    next({ ...to, replace: true })
}

//遍历后台传来的路由字符串，转换为组件对象
export function filterAsyncRouter(asyncRouterMap) {

    return asyncRouterMap.filter(route => {
        if (route.component) {
            if (route.component === 'Layout') { //Layout组件特殊处理
                route.component = Layout
            } else {
                route.component = _import(route.component)
            }
        }
        if (route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children)
        }

        return true
    })
}
